package com.bifrost.repository;

import com.bifrost.entity.Customer;
import com.bifrost.entity.PlanCusomer;
import com.bifrost.entity.PlanItem;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @author Lee
 * Create Class on 2018/8/13
 * @com.bifrost.repository
 * @wechat-mall
 */
public interface PlanCustomerRepository extends JpaRepository<PlanCusomer,Long>, JpaSpecificationExecutor<PlanCusomer>{

    Page<PlanCusomer> findAllByPlanItemAndStatus(PlanItem planItem , String status , Pageable pageable);

    Page<PlanCusomer> findAllByPlanItem(PlanItem planItem, Pageable request);

    PlanCusomer findFirstByPlanItemAndCustomer(PlanItem planItem, Customer customer);

    List<PlanCusomer> findAllByCustomer(Customer customer);

    @Query(value = "SELECT C.* FROM PLAN_CUSOMER C ,PLAN P WHERE C.PLAN_ID = P.PLAN_ID AND p.PLAN_STATUS <> 'END' AND C.CUSTOMER_ID=?1 AND C.STATUS=?2 AND P.ACTIVE_END_TIME>?3" , nativeQuery = true)
    List<PlanCusomer> findAllByCustomerAndStatus(Long customer, String status , Date date);

    List<PlanCusomer> findAllByPlanItemAndStatusAndCustomerFeedbackIsNotNull(PlanItem item, String wait , Pageable pageable);

    List<PlanCusomer> findAllByPlanItemAndStatus(PlanItem planItem, String hit);

    /** 已结束 */
    @Query(value = "SELECT DISTINCT C.* FROM PLAN_CUSOMER C ,PLAN P WHERE C.PLAN_ID = P.PLAN_ID AND C.CUSTOMER_ID=?1 AND (P.ACTIVE_END_TIME<?2 or p.PLAN_STATUS ='END') " , nativeQuery = true)
    List<PlanCusomer> findAllByCustomerNotIsActive(Long customerId, Date date);
}
